Lab9


4531208021_4531209621  นาย ชาตา ซาลวาลา และ นาย ฐณวัฒน์ คำนูณเศรษฐ์ (10/9/2545 (11:43:08))
(SM=1, CM=29, ST=33, KY=0, TR=06:46)

Applet
Source Code
public class DecimalLED extends SevenSegmentLED {
 public int temp ;

  public void setDigit(int d) {
    SevenSegmentLED led = new SevenSegmentLED();
    boolean[][] k = {{true, true, true, true, true, true, false}
      , {false, true, true, false, false, false, false}
      , {true, true, false, true, true, false, true}
      , {true, true, true, true, false, false, true}
      , {false, true, true, false, false, true, true}
      , {true, false, true, true, false, true, true}
      , {true, false, true, true, true, true, true}
      , {true, true, true, false, false, false, false}
      , {true, true, true, true, true, true, true}
      , {true, true, true, true, false, true, true}
      , {false, false, false, false, false, false, false}
      };
      if (d < 0 || d > 9) d = 10;
    boolean[] z = new boolean[7];
    for (int i = 0; i < 7; i++) {
      z[i] = k[d][i];
      }
    this.setSegments(z);
    temp = d;
    }
  public int getDigit() {
    if (temp < 0 || temp > 9) {
    return - 1;
    }
    else return temp;
  }  
}

import jlab.JLabIO; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Lab9 extends Applet implements Runnable { private DecimalLED[] display = new DecimalLED[3]; public void init() { for (int i = display.length - 1; i >= 0; i--) { display[i] = new DecimalLED(); add(display[i]); display[i].setSize(40, 80); } validate(); } public void start() { (new Thread(this)).start(); } public void run() { int k; display[0].setDigit(0); while (true) { for (int i = 0; i < display.length; i++) { k = Math.max(display[i].getDigit(), 0); display[i].setDigit((++k) % 10); if (k < 10) break; } try { Thread.sleep(100); } catch (InterruptedException e) {} } } public static void main(String[] args) { Frame frame = new Frame("Lab9 : 7 segments"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); Lab9 applet = new Lab9(); frame.add(applet, BorderLayout.CENTER); frame.setSize(200, 120); frame.setVisible(true); applet.init(); applet.start(); } }
import java.awt.*; public class SevenSegmentLED extends Canvas { private boolean[] segments; private static final int[] NUMPOINTS = {5, 5, 7, 5, 5, 5, 5}; private static final int[][][] SEGMENTS = { {{5, 35, 30, 10, 5, 0, 0}, {5, 5, 10, 10, 5, 0, 0}}, {{35, 35, 30, 30, 35, 0, 0}, {8, 37, 34, 13, 8, 0, 0}}, {{35, 35, 30, 30, 35, 0, 0}, {43, 72, 67, 46, 43, 0, 0}}, {{5, 35, 30, 10, 5, 0, 0}, {75, 75, 70, 70, 75, 0, 0}}, {{5, 5, 10, 10, 5, 0, 0}, {43, 72, 67, 46, 43, 0, 0}}, {{5, 5, 10, 10, 5, 0, 0}, {8, 37, 34, 13, 8, 0, 0}}, {{10, 30, 35, 30, 10, 5, 10}, {37, 37, 40, 43, 43, 40, 37}} }; public SevenSegmentLED() { setBackground(new Color(0, 0, 0)); setForeground(new Color(0, 255, 0)); } public void setSegments(boolean[] s) { if (s != null) { segments = new boolean[s.length]; for (int i = 0; i < s.length; i++) { segments[i] = s[i]; } repaint(); } } public void paint(Graphics g) { double scalex = (double) bounds().width / 40.0; double scaley = (double) bounds().height / 80.0; Color f = getForeground(); g.setColor(f); for (int i = 0; i < 7; i++) { if (segments != null && i < segments.length && segments[i]) { int[][] points = new int[2][NUMPOINTS[i]]; for (int j = 0; j < NUMPOINTS[i]; j++) { points[0][j] = (int) (SEGMENTS[i][0][j] * scalex); points[1][j] = (int) (SEGMENTS[i][1][j] * scaley); } g.fillPolygon(points[0], points[1], NUMPOINTS[i]); } } } }